//
//  TEXMLViewController.m
//  TintExample
//
//  Created by Kevin Wu on 4/21/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TEXMLViewController.h"



@implementation TEXMLViewController


- (void)loadView {
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = nil;
    NSData *data = nil;
    
    path = [[NSBundle mainBundle] pathForResource:@"test2.xml" ofType:nil];
    data = [NSData dataWithContentsOfFile:path];
    _xml1 = [[TTXML alloc] initWithXMLData:data];
    
    path = [[NSBundle mainBundle] pathForResource:@"test3.xml" ofType:nil];
    data = [NSData dataWithContentsOfFile:path];
    _xml2 = [[TTXML alloc] initWithXMLData:data];
    
    
    [self addButtonWithTitle:@"From head" selector:@selector(parseFromHead:)];
    [self addButtonWithTitle:@"From tail" selector:@selector(parseFromTail:)];
    
    [self addButtonWithTitle:@"Single from head" selector:@selector(parseSingleElementFromHead:)];
    [self addButtonWithTitle:@"Single from tail" selector:@selector(parseSingleElementFromTail:)];
    
    [self addButtonWithTitle:@"Attribute from head" selector:@selector(parseAttributeFromHead:)];
    [self addButtonWithTitle:@"Attribute from tail" selector:@selector(parseAttributeFromTail:)];
    
    [self addButtonWithTitle:@"All elements" selector:@selector(findAllElements:)];
    [self addButtonWithTitle:@"All same elements" selector:@selector(findAllSameElements:)];
    [self addButtonWithTitle:@"All attributes" selector:@selector(findAllAttributes:)];
    [self addButtonWithTitle:@"Single attribute" selector:@selector(findSingleAttribute:)];
    
    [self addButtonWithTitle:@"Create doc" selector:@selector(createXMLDoc:)];
}


- (void)parseFromHead:(id)sender {
    TTXMLNode *root = [_xml1 rootElement];
    TTXMLNode *node = [root firstChild];
    while (node) {
        NSLog(@"%@=%@", node.name, node.value);
        node = [node nextSibling];
    }
}

- (void)parseFromTail:(id)sender {
    TTXMLNode *root = [_xml1 rootElement];
    TTXMLNode *node = [root lastChild];
    while (node) {
        NSLog(@"%@=%@", node.name, node.value);
        node = [node previousSibling];
    }
}



- (void)parseSingleElementFromHead:(id)sender {
    TTXMLNode *root = [_xml1 rootElement];
    TTXMLNode *node = [root firstChildNamed:@"apple"];
    while (node) {
        NSLog(@"%@=%@", node.name, node.value);
        node = [node nextSameNameSibling];
    }
}

- (void)parseSingleElementFromTail:(id)sender {
    TTXMLNode *root = [_xml1 rootElement];
    TTXMLNode *node = [root lastChildNamed:@"apple"];
    while (node) {
        NSLog(@"%@=%@", node.name, node.value);
        node = [node previousSameNameSibling];
    }
}



- (void)parseAttributeFromHead:(id)sender {
    TTXMLNode *root = [_xml2 rootElement];
    TTXMLNode *attr = [root firstAttribute];
    while (attr) {
        NSLog(@"%@=%@", attr.name, attr.value);
        attr = [attr nextSibling];
    }
}

- (void)parseAttributeFromTail:(id)sender {
    TTXMLNode *root = [_xml2 rootElement];
    TTXMLNode *attr = [root lastAttribute];
    while (attr) {
        NSLog(@"%@=%@", attr.name, attr.value);
        attr = [attr previousSibling];
    }
}



- (void)findAllElements:(id)sender {
    TTXMLNode *root = [_xml1 rootElement];
    NSArray *elements = [root elements];
    for (int i=0; i<[elements count]; ++i) {
        TTXMLNode *node = [elements objectAtIndex:i];
        NSLog(@"%@=%@", node.name, node.value);
    }
}

- (void)findAllSameElements:(id)sender {
    TTXMLNode *root = [_xml1 rootElement];
    NSArray *elements = [root elementsForName:@"apple"];
    for (int i=0; i<[elements count]; ++i) {
        TTXMLNode *node = [elements objectAtIndex:i];
        NSLog(@"%@=%@", node.name, node.value);
    }
}

- (void)findAllAttributes:(id)sender {
    TTXMLNode *root = [_xml2 rootElement];
    NSArray *attributes = [root attributes];
    for (int i=0; i<[attributes count]; ++i) {
        TTXMLNode *node = [attributes objectAtIndex:i];
        NSLog(@"%@=%@", node.name, node.value);
    }
}

- (void)findSingleAttribute:(id)sender {
    TTXMLNode *root = [_xml2 rootElement];
    TTXMLNode *attr = [root attributeForName:@"height"];
    NSLog(@"%@=%@", attr.name, attr.value);
}



- (void)createXMLDoc:(id)sender {
    TTXML *xml = [TTXML XMLWithRootName:@"root"];
    TTXMLNode *root = [xml rootElement];
    
    TTXMLNode *element = nil;
    
    element = [root addElementWithName:@"e1" value:@"e1v"];
    [element addAttributeWithName:@"e1a1" value:@"e1a1v"];
    [element addAttributeWithName:@"e1a3" value:@"e1a3v"];
    [element addAttributeWithName:@"e1a2" value:@"e1a2v"];
    
    element = [root addTextWithValue:@"t"];
    
    element = [root addElementWithName:@"e1" value:@"e1v"];
    element = [root addElementWithName:@"e2" value:@"e2v"];
    
    NSLog(@"%s", [[xml XMLData] bytes]);
}

@end
